﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows;
using System.Xml;

namespace SHomeWorkshop.LunarMind.Commands
{
    /// <summary>
    /// 以此类为模板，制作自定义命令类
    ///     共需要修改１０处！！！
    /// ==============================
    /// 
    /// 全选命令。
    /// ★①，修改类名。
    /// </summary>
    public static class SetTitleIndentCommand
    {
        private static RoutedUICommand routedUICmd;//创建私有字段

        public static RoutedUICommand RoutedUICmd//创建只读静态属性
        {
            get { return routedUICmd; }
        }

        /// <summary>
        /// ★②，修改静态构造方法名。
        /// </summary>
        static SetTitleIndentCommand()//类型构造器
        {
            //★③，修改两个字符串参数名。★④以及第三个参数的类型名。
            routedUICmd = new RoutedUICommand(
                "SetTitleIndentCommand",
                "SetTitleIndentCommand",
                typeof(SetTitleIndentCommand),//创建RoutedUICommand对象
                null);

            cmdBinding.Command = routedUICmd;
            cmdBinding.CanExecute += new CanExecuteRoutedEventHandler(cmdBinding_CanExecute);
            cmdBinding.Executed += new ExecutedRoutedEventHandler(cmdBinding_Executed);
        }

        private static CommandBinding cmdBinding = new CommandBinding();

        public static CommandBinding CmdBinding
        {
            get { return cmdBinding; }
        }

        static void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //★⑦，修改此方法的实现。

            SetTitleIndent();
        }

        /// <summary>
        /// 此方法是调用DeleteCommand的Delete来实现。
        /// </summary>
        internal static void SetTitleIndent(int newIndent = 0)
        {
            if (Globals.MainWindow.SelectedTitlesList == null ||
                Globals.MainWindow.SelectedTitlesList.Count <= 0) return;

            bool haveFixedTitleSelected = false;
            foreach (Title t in Globals.MainWindow.SelectedTitlesList)
            {
                if (t.IsFixed)
                {
                    haveFixedTitleSelected = true;
                    break;
                }
            }

            if (haveFixedTitleSelected)
            {
                MessageBox.Show("选中的标题中有一个以上是FixedTitle。FixedTitle不支持设置缩进级别！",
                    Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            ModifingItem mi = new ModifingItem()
            {
                IsNeedRefreshTitleAndLinesLocation = true,
                ModifingName = "设置标题缩进"
            };

            Title rootTitle = Globals.MainWindow.RootTitle;

            foreach (Title t in Globals.MainWindow.SelectedTitlesList)
            {
                if (t == rootTitle)
                {
                    //MessageBox.Show("所选择的标题中包含了根标题。根标题不允许设置缩进级别，这一步操作(在根标题上)将被忽略。",
                    //    Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Information);
                    continue;
                }

                if (t.Indent == newIndent) continue;

                Action actIndent = new Action(t.ID, -1, t.GetType().Name, "Indent", t.Indent.ToString(),
                    newIndent.ToString());

                t.Indent = newIndent;

                mi.Add(actIndent);
            }

            if (mi.ModifiedPropertyAndValues.Count <= 0) return;

            Globals.MainWindow.Do(mi);
            Globals.MainWindow.IsModified = true;
            Globals.MainWindow.RefreshTitleAndLinesLocation();
        }

        static void cmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (Globals.MainWindow.SelectedTitlesList == null ||
                Globals.MainWindow.SelectedTitlesList.Count >= 0)
            {
                e.CanExecute = false; return;
            }
            else
            {
                bool haveFixedTitleSelected = false;
                foreach (Title t in Globals.MainWindow.SelectedTitlesList)
                {
                    if (t.IsFixed)
                    {
                        haveFixedTitleSelected = true;
                        break;
                    }
                }

                if (haveFixedTitleSelected)
                {
                    e.CanExecute = false; return;
                }
                else
                {
                    e.CanExecute = true; return;
                }
            }
        }
    }
}
